function format(json) {
    var ret = [], o = {};

    function add(arr, data) {
        var obj = {
            "id": data.id,
            "pid": data.pid,
            "name": data.name,
            "open": true,
            "children": [],
            "checked": data.checked
        };
        o[data.id] = obj;
        arr.push(obj);
    }

    json.forEach(function (x) {

        if (o[x.pid]) {
            add(o[x.pid].children, x);
        } else {
            add(ret, x);
        }
    });
    return ret;
}